package com.zd.service.impl;

import com.github.pagehelper.PageHelper;
import com.zd.mapper.CooperativeContentRelMapper;
import com.zd.mapper.QualificationsMapper;
import com.zd.mapper.WorkInfoMapper;
import com.zd.model.CooperativeContentRel;
import com.zd.model.WorkInfoStatistics;
import com.zd.model.Qualifications;
import com.zd.model.WorkInfo;
import com.zd.service.IWorkInfoService;
import com.zd.utils.FileUtil;
import com.zd.utils.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkInfoServiceImpl implements IWorkInfoService {

    //读取配置文件获取上传文件夹路径
    @Value("${upload.uploadPath}")
    private String uploadPath;

    @Autowired
    private WorkInfoMapper workInfoMapper;

    @Autowired
    private CooperativeContentRelMapper cooperativeContentRelMapper;

    @Autowired
    private QualificationsMapper qualificationsMapper;

    @Override
    public List<WorkInfo> selectWorkInfo(int pageNum, int pageSize, Map<String, Object> queryMap) {

        PageHelper.startPage(pageNum, pageSize);

        List<WorkInfo> workInfoList = workInfoMapper.selectWorkInfo(queryMap);

        return workInfoList;
    }

    @Override
    public List<WorkInfo> selectMaturity() {
        List<WorkInfo> timeList = workInfoMapper.selectMaturity();
        return timeList;
    }

    @Override
    public List<WorkInfoStatistics> selectWorkInfoStatistics(Map<String, Object> statisticsMap) {
        List<WorkInfoStatistics> workInfoStatisticsList = workInfoMapper.selectWorkInfoStatistics(statisticsMap);
        return workInfoStatisticsList;
    }


    @Override
    public boolean insertSelective(WorkInfo record) {

        workInfoMapper.insertSelective(record);

        //插入合作内容关联表
        List<CooperativeContentRel> cooperativeContentRelList = record.getCooperativeContentRelList();

        for (CooperativeContentRel cooperativeContentRel : cooperativeContentRelList) {

            if (cooperativeContentRel.getCooperativecontentid() != null && cooperativeContentRel.getCooperativecontentid().longValue() != -1) {
                cooperativeContentRel.setWorkinfoid(record.getId());
                cooperativeContentRelMapper.insertSelective(cooperativeContentRel);

            }


        }

        //插入企业资质表
        List<Qualifications> qualificationsList = record.getQualificationsList();
        if (qualificationsList != null && qualificationsList.size() != 0) {

            for (Qualifications qualifications : qualificationsList) {
                qualifications.setWorkinfoid(record.getId());

                //上传资质文件
                String uploadFileSingle = FileUtil.uploadFileSingle(qualifications.getQualificationsURLUpload(), uploadPath);

                qualifications.setQualificationsurl(uploadFileSingle);


                qualificationsMapper.insertSelective(qualifications);
            }

        }


        return true;
    }

    @Override
    public WorkInfo selectWorkInfoById(int id) {

        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("id", id);


        WorkInfo info = workInfoMapper.selectWorkInfo(queryMap).get(0);


        //拆分协议地址
        if (info.getAgreementurl() != null && !info.getAgreementurl().equals("")) {

            List<String> list = new ArrayList<String>();

            String[] split = info.getAgreementurl().split(",");

            for (String str : split) {

                list.add(str);

            }

            info.setAgreementurlList(list);
        }


        return info;
    }

    @Override
    public boolean updateSelective(WorkInfo workInfo) {

        //判断合作协议是否有值，如果有值表明有新上传的合作协议，累加修改数据库
        String agreementurl = workInfo.getAgreementurl();

        if (agreementurl != null && !agreementurl.equals("")) {
            //获取数据库原先合作协议值进行累加
            Map<String, Object> queryMap = new HashMap<String, Object>();
            queryMap.put("id", workInfo.getId());

            WorkInfo workInfoOld = workInfoMapper.selectWorkInfo(queryMap).get(0);

            workInfo.setAgreementurl(workInfoOld.getAgreementurl() + agreementurl);
        }

        //修改校企合作信息
        workInfoMapper.updateByPrimaryKeySelective(workInfo);

        List<CooperativeContentRel> cooperativeContentRelList = workInfo.getCooperativeContentRelList();

        if (cooperativeContentRelList != null && cooperativeContentRelList.size() != 0) {
            for (CooperativeContentRel cooperativeContentRel : cooperativeContentRelList) {

                //通过是否有主键id判断在修改过程中是否新增了数据，如果新增则插入数据库
                if (cooperativeContentRel.getId() == null || cooperativeContentRel.getId().longValue() == 0L) {

                    cooperativeContentRel.setWorkinfoid(workInfo.getId());
                    cooperativeContentRelMapper.insertSelective(cooperativeContentRel);
                } else {

                    cooperativeContentRelMapper.updateByPrimaryKeySelective(cooperativeContentRel);
                }

            }
        }

        try {


            //修改企业资质
            List<Qualifications> qualificationsList = workInfo.getQualificationsList();

            if (qualificationsList != null && qualificationsList.size() != 0) {

                for (Qualifications qualifications : qualificationsList) {

                    /**
                     * 页面中存在删除，隔空对象数组序列的情况，需要判断一个对象中所有属性都是空的情况
                     */
                    if (!ObjUtil.isAllFieldNull(qualifications)) {


                        //通过主键判断是否在修改过程中新增了企业资质，如果新增则插入数据
                        if (qualifications.getId() == null || qualifications.getId().longValue() == 0L) {

                            MultipartFile qualificationsURLUpload = qualifications.getQualificationsURLUpload();

                            String uploadFileSingle = FileUtil.uploadFileSingle(qualificationsURLUpload, uploadPath);

                            //插入上传资质的文件名
                            qualifications.setQualificationsurl(uploadFileSingle);

                            qualifications.setWorkinfoid(workInfo.getId());
                            qualificationsMapper.insertSelective(qualifications);
                        } else {
                            //当存在主键id 修改字段时，只涉及到资质名称
                            qualificationsMapper.updateByPrimaryKeySelective(qualifications);

                        }
                    }


                }


            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return true;
    }

    @Override
    public boolean deleteCooperativeContentRel(int id) {

        return cooperativeContentRelMapper.deleteByPrimaryKey(new Long(id)) > 0 ? true : false;
    }

    @Override
    public boolean deleteAgreementurlName(int id, String deleteAgreementurlName) {

        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("id", id);

        WorkInfo info = workInfoMapper.selectWorkInfo(queryMap).get(0);

        //获取数据库合作协议字段
        String agreementurls = info.getAgreementurl();

        //替换掉原先合作协议字符串
        String agreementurlReplace = agreementurls.replace(deleteAgreementurlName + ",", "");

        //修改数据库
        WorkInfo updateWorkInfo = new WorkInfo();

        updateWorkInfo.setId(new Long(id));

        updateWorkInfo.setAgreementurl(agreementurlReplace);

        workInfoMapper.updateByPrimaryKeySelective(updateWorkInfo);


        return true;
    }

    @Override
    public boolean deleteWorkInfo(int id) {

        WorkInfo workInfo = new WorkInfo();

        workInfo.setId(new Long(id));

        workInfo.setFlag(0);

        workInfoMapper.updateByPrimaryKeySelective(workInfo);

        return true;
    }

    @Override
    public boolean eliminateReminding(int id) {
        WorkInfo workInfo =new WorkInfo();
        workInfo.setId(new Long(id));
        List<WorkInfo>list=workInfoMapper.selectMaturity();
        for (WorkInfo tmp:list){
             if (tmp.getExpirationtips()==2&&tmp.getId()==id){
                 workInfo.setExpirationtips(0);
             }else if (tmp.getExpirationtips()==1&&tmp.getId()==id){
                workInfo.setExpirationtips(2);
             }
        }
        workInfoMapper.updateByPrimaryKeySelective(workInfo);
        return true;
    }


    @Override
    public List<WorkInfo> selectWorkExport(Map<String, Object> queryMap) {

        List<WorkInfo> workInfoList = workInfoMapper.selectWorkInfo(queryMap);

        return workInfoList;
    }

    @Override
    public boolean deleteLegalPersonCertificate(int id) {

        WorkInfo workInfo = new WorkInfo();

        workInfo.setId(new Long(id));

        //企业法人资质字段置空
        workInfo.setLegalPersonCertificateURL("");

        workInfoMapper.updateByPrimaryKeySelective(workInfo);


        return true;
    }

    @Override
    public boolean deleteBusinessLicense(int id) {

        WorkInfo workInfo = new WorkInfo();

        workInfo.setId(new Long(id));

        //营业执照字段置空
        workInfo.setBusinessLicenseURL("");

        workInfoMapper.updateByPrimaryKeySelective(workInfo);


        return true;
    }

    @Override
    public boolean deleteQualifications(int id) {

        qualificationsMapper.deleteByPrimaryKey(new Long(id));

        return true;
    }


}
